package  
{
import com.coltware.airxmail.ContentType;
import com.coltware.airxmail.INetAddress;
import com.coltware.airxmail.MailSender.SMTPSender;
import com.coltware.airxmail.MimeBinaryPart;
import com.coltware.airxmail.MimeMessage;
import com.coltware.airxmail.MimeTextPart;
import com.coltware.airxmail.RecipientType;
import flash.display.Sprite;
//import flash.filesystem.File;
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class SmtpTest extends Sprite
	{
		
		public function SmtpTest() 
		{
			//send_plain_email();
			//html_mail();
			attachments();
		}
		
	 
		private function send_plain_email():void{
			//  How to send plain text email
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST,"smtp.qq.com");
			sender.setParameter(SMTPSender.PORT,25);  // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH,true);
			sender.setParameter(SMTPSender.USERNAME,"db0");
			sender.setParameter(SMTPSender.PASSWORD,"biaoli512");

			// Create email message
			var message:MimeMessage = new MimeMessage();

			//  Set from email address and reciepients
			var from:INetAddress = new INetAddress("db0@qq.com","libiao");
			message.setFrom(from);

			var toRecpt:INetAddress = new INetAddress("db0@qq.com","libiao");
			message.addRcpt(RecipientType.TO,toRecpt);

			var ccRecpt:INetAddress = new INetAddress("e84@qq.com","libiao");
			message.addRcpt(RecipientType.CC,ccRecpt);

			//
			message.setSubject("hello world");
			message.setTextBody("welcom !! \r\n this is first email. ");

			sender.send(message);
			sender.close();
		}
		
		private function html_mail():void
		{
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST,"smtp.qq.com");
			sender.setParameter(SMTPSender.PORT,25);  // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH,true);
			sender.setParameter(SMTPSender.USERNAME,"db0");
			sender.setParameter(SMTPSender.PASSWORD, "libiaolibiao");
			
			var contentType:ContentType = ContentType.MULTIPART_ALTERNATIVE;
			var mimeMsg:MimeMessage = new MimeMessage(contentType);

			var from:INetAddress = new INetAddress();
			from.personal = "Sample User";
			from.address = "db0@qq.com";

			mimeMsg.setFrom(from);
			var toAddr:INetAddress = new INetAddress("db0@qq.com","Customer");
			mimeMsg.addRcpt(RecipientType.TO,toAddr);
			// set mail subject
			mimeMsg.setSubject("this is first my message");
			mimeMsg.setTextBody("this is multipart message");

			// TextPart
			var partText:MimeTextPart = mimeMsg.createTextPart();
			partText.setText("this is plain body");

			// HtmlPart
			var partHtml:MimeTextPart = mimeMsg.createTextPart();
			partHtml.setHtmlText("<html><body><b>THIS IS HTML BODY</b></body></html>");

			sender.send(mimeMsg);
			sender.close();
		}
		
		private function attachments():void
		{
			
			var sender:SMTPSender = new SMTPSender();
			sender.setParameter(SMTPSender.HOST,"smtp.qq.com");
			sender.setParameter(SMTPSender.PORT,25);  // default port is 25
			// If you use SMTP-AUTH
			sender.setParameter(SMTPSender.AUTH,true);
			sender.setParameter(SMTPSender.USERNAME,"db0");
			sender.setParameter(SMTPSender.PASSWORD, "libiaolibiao");
			
			var contentType:ContentType = ContentType.MULTIPART_ALTERNATIVE;
			var mimeMsg:MimeMessage = new MimeMessage(contentType);

			var from:INetAddress = new INetAddress();
			from.personal = "Sample User";
			from.address = "db0@qq.com";

			mimeMsg.setFrom(from);
			var toAddr:INetAddress = new INetAddress("db0@qq.com","Customer");
			mimeMsg.addRcpt(RecipientType.TO,toAddr);
			// set mail subject
			mimeMsg.setSubject("this is first my message");
			mimeMsg.setTextBody("this is multipart message");
			
			var contentType2:ContentType = ContentType.MULTIPART_MIXED;
			//var mimeMsg:MimeMessage = new MimeMessage(contentType2);
//
			var filePart:MimeBinaryPart = new MimeBinaryPart();
			//filePart.setAttachementFile(File.desktopDirectory.resolvePath("1.jpg"),"attachemnt-image.jpg");
			//mimeMsg.addChildPart(filePart);
//
			sender.send(mimeMsg);
			sender.close();
			
			
			//how to change content-type
			//var filePart:MimeBinaryPart = new MimeBinaryPart()
			//filePart.contentType.setMainType("image");
			//filePart.contentType.setSubType("jpeg");

			//Note: MimeImagePart is image/xxxx part.
			//filePart:MimeImagePart = new MimeImagePart();
			//filePart.setAttachementFile(file,"attachemnt-image.jpg");   // --> image/jpeg
			//filePart.setAttachementFile(file,"attachemnt-image.gif");   // --> image/gif
		}
	}

}